<template>
  <div class="haibei-home">
    <el-row :gutter="0">
      <el-col :xs="4" :sm="6" :md="4" :lg="4" :xl="3">
        <div class="home-left ">
          <div class="bg-blue-dark">
            <!--<img class="" src="/static/images/a01.png" alt="" width="40" height="40" style=" vertical-align: middle">-->
            <span class="white fs16 bold" style="margin-left: 20px;">&nbsp;经纪商后台管理系统</span>
          </div>
          <div :style="{height:height+'px',background:'#001529',overflow:'auto'}">
            <!-- <imenu-item :menu-list="menuList" :collect-list="collectList"></imenu-item> -->
            <el-menu
              :default-active="$route.name"
              background-color="#545c64"
              text-color="#fff"
              :router="true"
              active-text-color="#ffd04b">
              <el-menu-item :index="item.indexName" v-for="item in navList" :key="item.title">
                <i class="icon iconfont mr6" :class="item.className"></i>
                <span slot="title">{{item.title}}</span>
              </el-menu-item>

              <!--<el-menu-item index="pearlExchange">-->
              <!--<i class="el-icon-setting"></i>-->
              <!--<span slot="title">珍珠兑换列表</span>-->
              <!--</el-menu-item>-->
              <!-- <el-menu-item index="3">
               <i class="el-icon-setting"></i>
               <span slot="title">播单管理</span>
             </el-menu-item> -->
            </el-menu>


          </div>
        </div>
        <!-- {{getNavItem}} -->
      </el-col>
      <el-col :xs="20" :sm="18" :md="20" :lg="20" :xl="21">
        <div class="home-right">
          <div class="header-right-bar ">
            <div class="fl pl20" style="margin-top: 24px;">
              <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item :to="{name:'home'}">首页</el-breadcrumb-item>
                <!--<el-breadcrumb-item v-if="getNavItem.parentName">{{getNavItem.parentName}}</el-breadcrumb-item>-->
                <el-breadcrumb-item>{{$route.name | formatBreadcrumb}}</el-breadcrumb-item>
              </el-breadcrumb>
            </div>
            <div class="fr tr pr20">
              <img class="radius50" id="AG_head" :src="baseInfo.head_img" alt="" width="30" height="30" style=" vertical-align: middle">
              <span class="middle-grey ml5">{{baseInfo.name}}</span>
              <el-button type="text" class="ml10" plain size="mini" @click="isChangedPwd">修改密码</el-button>
              <el-button type="danger" plain size="mini" @click="loginOut">退出</el-button>
            </div>
            <div class="clear"></div>
          </div>
          <div class="home-content mt10 pl20 pr20" :style="{height: (height-10)+'px'}">
            <transition name="fade" mode="out-in">
              <router-view></router-view>
            </transition>
          </div>
        </div>
      </el-col>
    </el-row>
    <!--修改密码弹窗 begin-->
    <el-dialog
      width="30%"
      title="修改密码"
      :close-on-click-modal="isDialogClose"
      :show-close="isDialogClose"
      :visible.sync="dialogFormVisible">
      <el-form :model="changePwdForm" :rules="rules" ref="changePwdForm" label-width="80px" label-position="left">
        <el-form-item label="原始密码" prop="oldPwd">
          <el-input type="password" v-model="changePwdForm.oldPwd" placeholder="请输入原始密码"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPwd">
          <el-input type="password" v-model="changePwdForm.newPwd" placeholder="请输入新密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="surePwd">
          <el-input type="password" v-model="changePwdForm.surePwd" placeholder="请确认密码"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" :disabled="isPwdDisabled" @click="changePwd('changePwdForm')">确定</el-button>
        <el-button size="small" v-show="isShowDialogBtn" @click="dialogFormVisible = false">取 消</el-button>
      </div>
    </el-dialog>
    <!--修改密码 end-->
  </div>
</template>

<script>
import { getCookie,validatePassword,delCookie,setCookie } from "@/base/utils";
import service from "@/service/index";
import { mapGetters } from "vuex";
export default {
  name: "home",
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.changePwdForm.newPwd) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      height: 0,
      menuList: [],
      collectList: [],
      width: 0,
      isCollapse: false,
      leftWidth: 256,
      users: {},
      baseInfo:getCookie("baseInfo")||{},
      dialogFormVisible:false,
      isShowDialogBtn:false,
      isDialogClose:true,
      isPwdDisabled:false,  //提交按钮修改密码
      isShopShow:true,  //是否有商城权限
      isLivesShow:true,   //是否有直播权限
      navList:getCookie("navList")||{},
      changePwdForm:{
        oldPwd:'',
        newPwd:'',
        surePwd:''
      },
      rules:{
        oldPwd:[
          { required: true, message: '请输入原始密码', trigger: 'blur' },
          { min: 1, max: 21, message: '长度在 1 到 21 个字符', trigger: 'blur' }
        ],
        newPwd:[
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { validator: validatePassword, trigger: 'blur' }
        ],
        surePwd:[
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    };
  },
  computed: {
    ...mapGetters(["getNavItem"])
  },
  mounted() {
    this.setHeight();
    window.onresize = () => {
      this.setHeight();
    };
    // this.getMenuItemData();
    this.users = getCookie("userInfo") || {};
    this.getAgentList();  //查询经纪商
    this.getInit(); //是否第一次激活密码登陆
  },
  filters:{
    formatBreadcrumb(val){
      let arr=[{
        path: 'home',
        name: '主页管理',
      },{
        path: 'editHome',
        name: '首页编辑',
      },{
        path: 'pushed',
        name: '推送管理',
      },{
        path: 'shopping',
        name: '积分商城管理',
      },{
        path: 'shoppingEditPage',
        name: '编辑积分商城',
      },{
        path: 'shoppingView',
        name: '积分商城预览',
      },{
        path: 'reward',
        name: '打赏列表',
      },{
        path: 'apply',
        name: '积分消费审批',
      },{
        path: 'advert',
        name: '广告位管理',
      },{
        path: 'lives',
        name: '直播管理',
      },{
        path: 'pearlExchange',
        name: '',
      },{
        path: 'errorHtml',
        name: '404',
      }]
      for(let item of arr){
        if(item.path===val){
          return item.name
        }
      }
    }
  },
  methods: {
    getInit(){
      if(this.baseInfo.showUpdatePwd==true){
        this.dialogFormVisible=true;
        this.isShowDialogBtn=false;
        this.isDialogClose=false;
      }
    },
    async getMenuItemData() {
      let result = await service.haiheiServerMenuItem();
      if (result.success) {
        let list = result.result;
        this.menuList = list.data;
        this.collectList = list.collect;
        this.$store.dispatch("setAllMenu", list.data);
      } else {
        this.$message({
          type: "error",
          message: "获取失败"
        });
      }
    },
    setHeight() {
      let win_height = document.documentElement.clientHeight || document.body.clientHeight;
      let win_width = document.documentElement.clientWidth || document.body.clientWidth;
      this.height = win_height - 61;
      this.width = win_width - 275;
    },
    loginOut() {
      this.$confirm("您确定退出系统吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        lockScroll: false
      }).then(() => {
          service.haiBeiServerLoginOut({}).then(res => {
            if (res.success) {
              delCookie("userInfo");
              this.$router.push({name: "login"});
            }else{
              this.$message({type:'error', message:res.msg});
              return;
            }
          });
        })
        .catch(() => {});
    },
    isChangedPwd(){
      this.dialogFormVisible=true;
      if(this.baseInfo.showUpdatePwd==true){
        this.isShowDialogBtn=false;
        this.isDialogClose=false;
      }else {
        this.isShowDialogBtn=true;
        this.isDialogClose=true;
      }
    },
    changePwd(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.isPwdDisabled = true;
          let params = {
            old_password:this.changePwdForm.oldPwd,
            new_password:this.changePwdForm.surePwd
          }
          service.haiBeiServerEditPassword(params).then(res => {
            if (res.success) {
              //console.log(JSON.stringify(params,'-----'));
              setTimeout(()=>{
                this.isPwdDisabled = false;
              },1000);
              delCookie("userInfo");
              this.$message({type:'success', message:res.msg});
              this.dialogFormVisible = false;
              this.$router.push({name: "login"});
            }else{
              this.isPwdDisabled = false;
              this.$message({type:'error', message:res.msg});
              return;
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    getAgentList(){
      service.haiBeiServerQueryAgentList({}).then(res => {
        if (res.success) {
          setCookie('agentList',res.result);
          //console.log(JSON.stringify(res),'-------agentList');
          //this.$message({type:'success', message:res.msg});
        }else{
          this.$message({type:'error', message:res.msg});
          return;
        }
      });
    }
  },
  components: {
    // imenuItem
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.el-menu {
  border-right: 0;
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 256px;
}

.home {
  height: 100vh;
  width: 100%;
  background: #fff;
}

.haibei-home {
  width: 100%;
  min-width: 1300px;
}

.home-content {
  background: #f1f1f1;
  margin: 10px auto;
  overflow-y: auto;
}

.home-left {
  box-shadow: 0px 2px 10px #000;
  overflow: hidden;
  /* width:256px; */
}

.home-right {
  min-width: 1030px;
  background: #f0f2f5;
  height: 100vh;
  overflow: hidden;
}

.bg-blue-dark {
  background: #001529;
  height: 60px;
  line-height: 60px;
}

.header-right-bar {
  height: 60px;
  line-height: 60px;
  padding: 0 12px 0 0;
  background: #fff;
  -webkit-box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  -moz-box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}

.breadcrumb-nav {
  margin-top: 6px;
  background: #fff;
  padding: 20px;
  border-bottom: 1px solid #e8e8e8;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s;
}

.fade-enter,
.fade-leave-to {
  opacity: 0;
}
</style>
